package com.zd.lwsq.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.zd.lwsp.domain.AppInfoProcess;
import com.zd.lwsp.service.AppInfoProcessService;
import com.zd.lwsq.service.ApplicationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.lwsq.mapper.ApplicationInfoMapper;
import com.zd.lwsq.domain.ApplicationInfo;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 离乌申请数据Service业务层处理
 *
 * @author fxiao
 * @date 2020-08-07
 */
@Service("ApplicationInfoServiceImpl")
public class ApplicationInfoServiceImpl implements ApplicationInfoService
{
    @Autowired
    private ApplicationInfoMapper applicationInfoMapper;

    @Autowired
    DictService dictService;

    @Autowired
    private AppInfoProcessService appInfoProcessService;

    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    /**
     * 查询离乌申请数据
     *
     * @param id 离乌申请数据ID
     * @return 离乌申请数据
     */
    @Override
    public ApplicationInfo selectApplicationInfoById(String id)
    {
        return applicationInfoMapper.selectApplicationInfoById(id);
    }

    /**
     * 查询离乌申请数据列表
     *
     * @param applicationInfo 离乌申请数据
     * @return 离乌申请数据
     */
    @Override
    public List<ApplicationInfo> selectApplicationInfoList(ApplicationInfo applicationInfo)
    {
        return applicationInfoMapper.selectApplicationInfoList(applicationInfo);
    }

    /**
     * 新增离乌申请数据
     *
     * @param applicationInfo 离乌申请数据
     * @return 结果
     */
    @Transactional
    @Override
    public int insertApplicationInfo(ApplicationInfo applicationInfo)
    {
        String randomID = UUID.randomUUID().toString().replaceAll("-","");
        String randomID2 = UUID.randomUUID().toString().replaceAll("-","");
        applicationInfo.setId(randomID);
        //先保存一个状态对象
        AppInfoProcess appInfoProcess=new AppInfoProcess();
        appInfoProcess.setYwdxId(applicationInfo.getId());
        appInfoProcess.setYwdxType("ApplicationInfo");
        appInfoProcess.setId(randomID2);
        appInfoProcess.setYwdxSsSqId(applicationInfo.getSqCode());
        appInfoProcessService.insertAppInfoProcess(appInfoProcess);
        //再保存离乌申请对象
        SysDept sqDept=sysDeptService.selectDeptById(Long.valueOf(applicationInfo.getSqCode()));
        SysDept jdbDept=sysDeptService.selectDeptById(sqDept.getParentId());
        SysDept qjDept=sysDeptService.selectDeptById(jdbDept.getParentId());
        applicationInfo.setJdbCode(jdbDept.getDeptId().toString());
        applicationInfo.setQjCode(qjDept.getDeptId().toString());
        return applicationInfoMapper.insertApplicationInfo(applicationInfo);
    }

    /**
     * 修改离乌申请数据
     *
     * @param applicationInfo 离乌申请数据
     * @return 结果
     */
    @Override
    public int updateApplicationInfo(ApplicationInfo applicationInfo)
    {
        if(null!=applicationInfo.getSqCode()) {
            SysDept sqDept = sysDeptService.selectDeptById(Long.valueOf(applicationInfo.getSqCode()));
            SysDept jdbDept = sysDeptService.selectDeptById(sqDept.getParentId());
            SysDept qjDept = sysDeptService.selectDeptById(jdbDept.getParentId());
            applicationInfo.setJdbCode(jdbDept.getDeptId().toString());
            applicationInfo.setQjCode(qjDept.getDeptId().toString());
        }
        return applicationInfoMapper.updateApplicationInfo(applicationInfo);
    }

    /**
     * 删除离乌申请数据对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteApplicationInfoByIds(String ids)
    {
        return applicationInfoMapper.deleteApplicationInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除离乌申请数据信息
     *
     * @param id 离乌申请数据ID
     * @return 结果
     */
    @Override
    public int deleteApplicationInfoById(String id)
    {
        return applicationInfoMapper.deleteApplicationInfoById(id);
    }

    @Override
    public void translation(List<ApplicationInfo> applicationInfoList) {
        Map<String,List<SysDictData>> map = new HashMap<>();
        map.put("b_spzt",iSysDictTypeService.selectDictDataByType("b_spzt"));
        map.put("sys_user_sex",iSysDictTypeService.selectDictDataByType("sys_user_sex"));
        map.put("sys_yes_no",iSysDictTypeService.selectDictDataByType("sys_yes_no"));
        map.put("b_zjlx",iSysDictTypeService.selectDictDataByType("b_zjlx"));
        map.put("b_zzslx",iSysDictTypeService.selectDictDataByType("b_zzslx"));
        map.put("b_ssjdb",iSysDictTypeService.selectDictDataByType("b_ssjdb"));
        map.put("b_jtfs",iSysDictTypeService.selectDictDataByType("b_jtfs"));
        map.put("b_rylx",iSysDictTypeService.selectDictDataByType("b_rylx"));
        for (int i = 0; i < applicationInfoList.size(); i++) {
            ApplicationInfo applicationInfo=applicationInfoList.get(i);
            if(StringUtils.isNotEmpty(applicationInfo.getSpzt())){
                for (SysDictData b_spzt : map.get("b_spzt")) {
                    if (b_spzt.getDictValue().equals(applicationInfo.getSpzt())){
                        applicationInfo.setSpzt(b_spzt.getDictLabel());
                    }
                }
            }
            if(StringUtils.isNotEmpty(applicationInfo.getXb())){

                for (SysDictData sys_user_sex : map.get("sys_user_sex")) {
                    if (sys_user_sex.getDictValue().equals(applicationInfo.getXb())){
                        applicationInfo.setXb(sys_user_sex.getDictLabel());
                    }
                }

            }
            if(StringUtils.isNotEmpty(applicationInfo.getStyc())){
                for (SysDictData sys_yes_no : map.get("sys_yes_no")) {
                    if (sys_yes_no.getDictValue().equals(applicationInfo.getStyc())){
                        applicationInfo.setStyc(sys_yes_no.getDictLabel());
                    }
                }
            }
            if(StringUtils.isNotEmpty(applicationInfo.getZjlx())){
                for (SysDictData b_zjlx : map.get("b_zjlx")) {
                    if (b_zjlx.getDictValue().equals(applicationInfo.getZjlx())){
                        applicationInfo.setZjlx(b_zjlx.getDictLabel());
                    }
                }
            }
            if(StringUtils.isNotEmpty(applicationInfo.getZzslx())){
                for (SysDictData b_zzslx : map.get("b_zzslx")) {
                    if (b_zzslx.getDictValue().equals(applicationInfo.getZzslx())){
                        applicationInfo.setZzslx(b_zzslx.getDictLabel());
                    }
                }
            }
            if(StringUtils.isNotEmpty(applicationInfo.getSqName())){
                for (SysDictData b_ssjdb : map.get("b_ssjdb")) {
                    if (b_ssjdb.getDictValue().equals(applicationInfo.getSqName())){
                        applicationInfo.setSqName(b_ssjdb.getDictLabel());
                    }
                }
            }
            if(StringUtils.isNotEmpty(applicationInfo.getLwfs())){
                for (SysDictData b_jtfs : map.get("b_jtfs")) {
                    if (b_jtfs.getDictValue().equals(applicationInfo.getLwfs())){
                        applicationInfo.setLwfs(b_jtfs.getDictLabel());
                    }
                }
            }
            if(StringUtils.isNotEmpty(applicationInfo.getHsjc())){
                for (SysDictData sys_yes_no : map.get("sys_yes_no")) {
                    if (sys_yes_no.getDictValue().equals(applicationInfo.getHsjc())){
                        applicationInfo.setHsjc(sys_yes_no.getDictLabel());
                    }
                }
            }
            if(StringUtils.isNotEmpty(applicationInfo.getSkjc())){
                for (SysDictData sys_yes_no : map.get("sys_yes_no")) {
                    if (sys_yes_no.getDictValue().equals(applicationInfo.getSkjc())){
                        applicationInfo.setSkjc(sys_yes_no.getDictLabel());
                    }
                }
            }
            if(StringUtils.isNotEmpty(applicationInfo.getSfjn())){
                for (SysDictData b_rylx : map.get("b_rylx")) {
                    if (b_rylx.getDictValue().equals(applicationInfo.getSfjn())){
                        applicationInfo.setSfjn(b_rylx.getDictLabel());
                    }
                }
            }
            applicationInfoList.set(i,applicationInfo);
        }
    }

    @Override
    public ApplicationInfo selectApplicationInfoByTel(String tel) {
        return applicationInfoMapper.selectApplicationInfoByTel(tel);
    }

    @Override
    public boolean check(ApplicationInfo applicationInfo) {
        ApplicationInfo params=new ApplicationInfo();
        params.setZjlx(applicationInfo.getZjlx());
        params.setZjhm(applicationInfo.getZjhm());
        List<ApplicationInfo>result=applicationInfoMapper.selectApplicationInfoList(params);
        if(result!=null&&result.size()>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public ApplicationInfo selectApplicationInfoByCardId(String id) {
        return applicationInfoMapper.selectApplicationInfoByCardId(id);
    }

    /*@Override
    public List<ApplicationInfo> selectBySqIds(List<Long> sqIds) {
        return applicationInfoMapper.selectBySqIds(sqIds);
    }*/


}
